//
//  BaseDetailsController.m

//
//  Created by dllo on 15/10/7.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "ZGLHotDetailsVC.h"
#import "ZGLHTTPTOOL.h"
#import "ZGLHotDetailsModel.h"

@interface ZGLHotDetailsVC ()
@property(nonatomic, retain)UIWebView *web;
@property(nonatomic, copy)NSString *webString;
@property(nonatomic, copy)NSString *webTitle;
@property(nonatomic, copy)NSString *webSource;
@property(nonatomic, retain)UIActivityIndicatorView* activityIndicatorView;
@end
@implementation ZGLHotDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = backColor;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zaihufanhui"] style:(UIBarButtonItemStyleDone) target:self action:@selector(leftBarButtonItem:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    _activityIndicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0,0,150,150)];
    _activityIndicatorView.center = self.view.center;
    _activityIndicatorView.activityIndicatorViewStyle= UIActivityIndicatorViewStyleWhiteLarge;
    [_activityIndicatorView setColor:naviColor];
    [self.view addSubview:_activityIndicatorView];
    [_activityIndicatorView startAnimating];
    
    [self handData];
}

- (void)leftBarButtonItem:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)handData{
    __weak typeof(self) weakSelf = self;
    [ZGLHTTPTOOL GETWithURL:[NSString stringWithFormat:@"http://c.m.163.com/nc/article/%@/full.html", self.docid] withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
        [weakSelf.activityIndicatorView stopAnimating];
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        [weakSelf handleWebString:tempDic];
        [weakSelf createWebView];
    } withFail:^(NSError *error) {
        NSLog(@"没网:ZGLHotDEtailsVC");
    }];
}

- (void)handleWebString:(NSDictionary *)dic{
    ZGLHotDetailsModel *mod = [ZGLHotDetailsModel modWithDic:[dic objectForKey:self.docid]];
    _webString = mod.body;
    _webTitle = mod.title;
    _webSource = [NSString stringWithFormat:@"%@       %@", mod.source, mod.ptime];
    for (NSDictionary *dic in mod.img) {
        _webString = [_webString stringByReplacingOccurrencesOfString:[dic objectForKey:@"ref"] withString:[NSString stringWithFormat:@"\
                            <div>\
                            <img src=\"%@\"/ width=\"100%%\">\
                            <p style=\"color:gray;font-size:80%%\">\
                            %@\
                            </p>\
                            </div>",[dic objectForKey:@"src"], [dic objectForKey:@"alt"]]];
    }
}

- (void)createWebView{
    _web = [[UIWebView alloc]init];
    _web.backgroundColor = backColor;
    _web.scrollView.bounces = NO;
    _web.frame = CGRectMake(0, 64, Width, Height-65);
    NSString *night = @"";
    if ([ZGLsettings shareSettings].isNight) {
        night = @"bgcolor=\"#4D4D4D\" text=\"white\" ";
    }else{
        night = @"bgcolor=\"#ffffff\" text=\"black\" ";
    }
    [self.web loadHTMLString:[NSString stringWithFormat:@"\
                            <!DOCTYPE HTML>\
                            <html>\
                            <head>\
                            </head>\
                            <style type=\"text/css\">\
                            #head {\
                                height:100px;\
                                }\
                            </style>\
                            <body %@>\
                            <div id=\"head\">\
                            <h2>%@</h2>\
                            <h4>%@</h4>\
                            </div>\
                            %@\
                            </body> </html>", night, _webTitle, _webSource, _webString] baseURL:nil];
    [self.view addSubview:_web];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

